package com.cloudstudy.tool;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

public class SpringTool {
	public static ServletContext servletContext;// 在系统启动时赋�?

	public static Object getBean(HttpServletRequest request, String beanName) {
		ApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());
		Object bean = ctx.getBean(beanName);
		return bean;
	}

	public static <T> T getSpringBean(HttpServletRequest request,
			String beanName) {
		ApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(request.getSession()
						.getServletContext());
		T bean = (T) ctx.getBean(beanName);
		return bean;
	}

	public static <T> T getSpringBean(String beanName){
		T bean = null;
		ApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);

		if (ctx.getBean(StringUtils.uncapitalize(beanName)) != null) {
			bean = (T)ctx.getBean(StringUtils.uncapitalize(beanName));
		}
		
		return bean;
	}
	
	public static <T> T getSpringBeanNoThrow(String beanName){
		T bean = null;
		ApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
		
		try {
			if (ctx.getBean(StringUtils.uncapitalize(beanName)) != null) {
				bean = (T)ctx.getBean(StringUtils.uncapitalize(beanName));
			}
		} catch (Exception e) {
			LogTool.debug(SpringTool.class, "getSpringBeanNoThrow, can not get bean:"+beanName);
		}
		
		return bean;
	}
	public static <T> T getSpringBeanThrows(String beanName) throws Exception{
	
		if (getSpringBean(beanName) == null) {
			throw new Exception("can not get bean:"+beanName);
		}
		T bean = (T)getSpringBean(beanName) ;
		return bean;
	}
	
	public static <T> T getSpringBean(Class<T> t) {
		String beanName = StringUtils.uncapitalize(t.getSimpleName());
		ApplicationContext ctx = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
		if (ctx.getBean(beanName) == null) {
			return null;
		}
		T bean = (T)ctx.getBean(beanName) ;
		return bean;
	}

	public static Object getSpringBeanByXml(String springXmlPath,
			String beanName) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { springXmlPath });
		BeanFactory factory = (BeanFactory) context;

		Object bean = factory.getBean(beanName);
		return bean;
	}

	public static Object getSpringBeanByXml(String[] xmlPath, String beanName) {
		ApplicationContext context = new ClassPathXmlApplicationContext(xmlPath);
		BeanFactory factory = (BeanFactory) context;

		Object bean = factory.getBean(beanName);
		return bean;
	}

	public static <T> T getBeanByXml(String springXmlPath, String beanName) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { springXmlPath });
		BeanFactory factory = (BeanFactory) context;

		T bean = (T) factory.getBean(beanName);
		return bean;
	}

	public static <T> T getBeanByXml(String springXmlPath, Class<T> t) {
		String beanName = StringUtils.uncapitalize(t.getSimpleName());
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { springXmlPath });
		BeanFactory factory = (BeanFactory) context;
		T bean = (T) factory.getBean(beanName);
		return bean;
	}

	public static ServletContext getServletContext() {
		return servletContext;
	}

	public static void setServletContext(ServletContext servletContext) {
		SpringTool.servletContext = servletContext;
	}

	public static void main(String[] args) {
//		String springXmlPath = "applicationContext-test.xml";
//		String beanName = StringUtils.uncapitalize(TestBean.class.getSimpleName());
//		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { springXmlPath });
//		BeanFactory factory = (BeanFactory) context;
//
//		
//		TestBean bean = (TestBean)factory.getBean(beanName);
//		System.out.println(bean);
//		bean.show();
//		bean = null;
////		bean = SpringTool.getBeanByXml("applicationContext-test.xml",TestBean.class);
//		TestBean bean2 = (TestBean)factory.getBean(beanName);
//		System.out.println(bean2);
//		bean2.show();
	}

}
